package com.you.atlas.web.place.module.action;

import java.io.IOException;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.Group;
import com.alibaba.citrus.service.uribroker.URIBrokerService;
import com.alibaba.citrus.service.uribroker.uri.GenericURIBroker;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.util.FileUtil;
import com.alibaba.citrus.util.StringUtil;
import com.you.atlas.biz.place.service.NotePhotoService;
import com.you.common.file.ImageService;
import com.you.common.webx.AuthContext;
import com.you.place.dal.dao.dataobject.NotePhotoDO;

public class NotePhotoAction {

    @Autowired
    private ImageService     imageService;

    @Autowired
    private NotePhotoService notePhotoService;

    @Autowired
    private URIBrokerService uriBrokerService;

    public void doUpload(@FormGroup("uploadImg") Group group, @Param("noteItemId") Long noteItemId, Context context,
                         Navigator navigator) {
        FileItem fileItem = group.getField("imgUrl").getFileItem();
        if (fileItem != null) {
            try {
                String imgUrl = imageService.scaleImg(fileItem.getInputStream(), fileItem.getName(), 600, 450);
                String thumbImgUrl = imageService.scaleIcon(imgUrl, 96, 80);
                context.put("imgUrl", imgUrl);
                context.put("thumbImgUrl", thumbImgUrl);

                FileUtil.FileNameAndExtension fileNameAndExtension = FileUtil.getFileNameAndExtension(fileItem.getName());
                String photoName = fileNameAndExtension.getFileName();
                photoName = photoName.substring(0, 5) + "." + fileNameAndExtension.getExtension();
                photoName = StringUtil.toCamelCase(photoName);
                photoName = "[" + photoName + "]";

                NotePhotoDO notePhotoDO = new NotePhotoDO();
                notePhotoDO.setNoteItemId(noteItemId);

                notePhotoDO.setImgUrl(broke(imgUrl));
                notePhotoDO.setThumbImgUrl(broke(thumbImgUrl));
                notePhotoDO.setName(photoName);
                notePhotoDO.setMemberId(AuthContext.getContext().getMemberId());
                notePhotoService.createNotePhoto(notePhotoDO);

                navigator.redirectTo("placeModule").withTarget("notePhotoView.vm").withParameter("noteItemId",
                                                                                                 noteItemId == null ? "" : noteItemId.toString()).withParameter("uploadPhotoName",
                                                                                                                                                     photoName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String broke(String imgUrl) {
        GenericURIBroker uriBroker = (GenericURIBroker) uriBrokerService.getURIBroker("uploadLink");
        return uriBroker.addPath(imgUrl).render();
    }
}
